package 耿翔宇;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.Scanner;

import cn.hutool.core.io.FileUtil;

public class CallName04 {

	public static void main(String[] args) {
		File called = new File("called.txt");
		// ��ʼ����called����
		FileUtil.writeLines(new ArrayList<>(), called, "utf-8",false);
		List<String> students = new ArrayList<>();  
		List<String> name = new ArrayList<>();  
        Random random = new Random();  
        int i = 1,j=0;
        try {  
            while (true) {  
                if (students.isEmpty()) {// һ�ֵ����ˣ����»�ȡ��������ʼ��һ��
                	System.out.println("��"+i+"��");
                    students = reloadStudents(students); 
                    i++;
                    j=0;
                }  
                // ���ѡ��һ��ѧ��  
                int index = random.nextInt(students.size());  
                String calledStudent = students.remove(index);
                System.out.printf("��%d���㵽���ǣ�%s\n",++j,calledStudent);
                // ׷�ӵ�called��
                name.add(calledStudent);
                FileUtil.writeLines(name, called, "utf-8",true);
                name.clear();
                
                Scanner sc = new Scanner(System.in);
                System.out.println("���롮1��������������ֹ");
                String s = sc.next();
                if(!s.equals("1")){              	
                	System.out.println("������");
                	return;
                }
            }  
        } catch (IOException e) {  
            e.printStackTrace();  
        } 
	}

	// ÿһ�����¼�������  
    private static List<String> reloadStudents(List<String> students) throws IOException {
    	// ��ȡcalled����
        List<String> called = FileUtil.readLines(new File("called.txt"), "utf-8");
        // ��ȡ��������
        List<String> names = FileUtil.readLines(new File("names.txt"), "utf-8");
        // ��ȡδ���㵽������
        names.removeAll(called);
        called.clear();
        // ��ȡ10�����µĴ���������
        Collections.shuffle(names);
        names.stream().limit(10).forEach(c->called.add(c));
        FileUtil.writeLines(called,new File("randomcall04.txt"), "utf-8",false);
        List<String> nextNames = FileUtil.readLines(new File("randomcall04.txt"), "utf-8"); 
        return nextNames;
    }  
	
}
